package com.ljw.test.view;

import android.content.Context;
import android.os.Build;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;

import androidx.annotation.RequiresApi;

import com.ljw.test.R;

import java.util.LinkedList;
import java.util.List;


public class ChildAdapter extends BaseAdapter {
    private List<String> childList;
    private List<Boolean> childState;
    private Context context;

    public ChildAdapter(Context context) {
        this.context = context;

        childList = new LinkedList<>();
        childState = new LinkedList<>();

        childList.add("");
        childState.add(false);
    }

    @Override
    public int getCount() {
        return childList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.item_list_child, null);
            holder = new ViewHolder(convertView);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.etItemAddChild.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String childString = s.toString().trim();

                if (!childState.get(position))
                    childList.set(position, childString);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        holder.btItemNextLine.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.N)
            @Override
            public void onClick(View v) {
                if (!holder.etItemAddChild.getText().toString().equals("")) {
                    childList.add("");
                    childState.add(position, true);
                    holder.btItemNextLine.setVisibility(View.GONE);

                    notifyDataSetChanged();
                }
            }
        });
        return convertView;
    }

    public List<String> getChildList() {
        return childList;
    }

    class ViewHolder {
        public View rootView;
        public EditText etItemAddChild;
        public Button btItemNextLine;

        public ViewHolder(View rootView) {
            this.rootView = rootView;
            this.etItemAddChild = (EditText) rootView.findViewById(R.id.et_item_add_child);
            this.btItemNextLine = (Button) rootView.findViewById(R.id.bt_item_next_line);
        }
    }
}
